using System;
using System.Windows.Forms;
using MEA.AE_Data;
using MEA.AE_Data.Config;

namespace MEA.AE_BusinessLogic
{
	public class FlashConditionEvaluator : IFlashConditionEvaluator
	{
		private struct TestMotNr
		{
			private string[] test;

			public bool testcMotorSerialNumber(string EngineType, string min, string max)
			{
				bool result = false;
				if (testEngineSerial(EngineType))
				{
					test = EngineType.Split('-');
					if (Convert.ToUInt32(min) <= Convert.ToUInt32(test[2]) && Convert.ToUInt32(test[2]) <= Convert.ToUInt32(max))
					{
						result = true;
					}
				}
				return result;
			}

			private bool testEngineSerial(string strEngSer)
			{
				bool flag = false;
				string[] array = strEngSer.Split('-');
				if (array[0].StartsWith("E4") && array[0].Length < 4 && array[1].Length == 1 && (array[2].Length == 4 || array[2].Length == 5))
				{
					flag = true;
					string text = array[2];
					foreach (char c in text)
					{
						flag = flag && c >= '0' && c <= '9';
					}
				}
				return flag;
			}
		}

		private struct ECUSwData
		{
			public ushort ecu_SW_Version;

			public ushort ecu_SW_Revision;

			public double ecu_DS_Version;

			public char motorSWType;

			public string motorVariante;

			public string softvareVariante;

			private bool isCharDigit(string a)
			{
				bool result = true;
				foreach (char c in a)
				{
					if (c < '0' || c > '9')
					{
						result = false;
					}
				}
				return result;
			}

			private bool isChar(string a)
			{
				bool result = true;
				foreach (char c in a)
				{
					if ((c < 'A' || c > 'Z') && (c < 'a' || c > 'z'))
					{
						result = false;
					}
				}
				return result;
			}

			private int digitLen(string str)
			{
				int result = str.Length;
				bool flag = true;
				for (int i = 0; i < str.Length; i++)
				{
					if (!flag)
					{
						break;
					}
					if (str[i] < '0' || str[i] > '9')
					{
						flag = false;
						result = i;
					}
				}
				return result;
			}

			public bool readSwVersion(string swver)
			{
				bool result = false;
				motorVariante = "";
				softvareVariante = "";
				string[] array = swver.Split('_');
				int num = array.Length;
				string text = array[0].Replace("VC", "");
				if (isCharDigit(text))
				{
					ecu_SW_Version = Convert.ToUInt16(text);
					text = array[1];
					if (text.Length < 5)
					{
						int num2 = digitLen(text);
						int num3 = Convert.ToInt32(text.Substring(0, num2));
						if (num2 < text.Length)
						{
							motorVariante = text.Substring(num2, 1);
						}
						if (num3 < ConfigData.MotorType.Length)
						{
							motorSWType = ConfigData.MotorType[num3];
							if (isCharDigit(array[2]))
							{
								ecu_SW_Revision = Convert.ToUInt16(array[2]);
								if (num == 4)
								{
									string[] array2 = array[3].Split(' ');
									if (array2[0].Length > 3)
									{
										string[] array3 = array2[0].Split('-');
										ecu_DS_Version = Convert.ToDouble(array3[0]);
									}
									else
									{
										ecu_DS_Version = Convert.ToDouble(array2[0]);
									}
									result = true;
								}
								else if (num > 4)
								{
									ecu_DS_Version = Convert.ToDouble(array[3]);
									string[] array2 = array[4].Split(' ');
									softvareVariante = array2[0];
									result = true;
								}
							}
						}
					}
				}
				return result;
			}
		}

		private FlashContainer flash_Container;

		private IECU_Data ecuA;

		private IECU_Data ecuB;

		private IEngine12 engA;

		private IEngine12 engB;

		private TestMotNr testmotornr;

		private ECUSwData ecuSWData;

		private float fctVer;

		public FlashContainer Flash_Container
		{
			get
			{
				return flash_Container;
			}
			set
			{
				flash_Container = value;
			}
		}

		public FlashConditionEvaluator(IECU_Data ecuA, IECU_Data ecuB, IEngine12 engA, IEngine12 engB)
		{
			this.ecuA = ecuA;
			this.ecuB = ecuB;
			this.engA = engA;
			this.engB = engB;
		}

		public bool Evaluate_FlashConditions(ConfigData.e_ECUID ecuID, ref FlashContainer fct, ref string exitReason)
		{
			flash_Container = fct;
			IECU_Data iECU_Data;
			IEngine12 eng;
			switch (ecuID)
			{
			case ConfigData.e_ECUID.A:
				iECU_Data = ecuA;
				eng = engA;
				break;
			case ConfigData.e_ECUID.B:
				iECU_Data = ecuB;
				eng = engB;
				break;
			default:
				MessageBox.Show("Invalid ECU ID selected.\r\nFlash process aborted!", ConfigData.WizardSwName);
				exitReason += "Flash-Conditions: Invalid ECU ID selected.\r\nFlash process aborted!";
				return false;
			}
			bool flag = true;
			if (!Evaluate_FCTVersion(fct.File_fct_version))
			{
				exitReason = string.Format("Current FCT version not supported for update by this flash container, must be in range {0} till {1}\r\nFlash process aborted!", 0.3, 1.0);
				if (flag)
				{
					MessageBox.Show(exitReason, ConfigData.WizardSwName);
				}
				return false;
			}
			if (!Evaluate_HWVersion(iECU_Data))
			{
				exitReason = string.Format("HW version not supported, must be in range {0} to {1}\r\nFlash process aborted!", flash_Container.Conditions_ecu_hw_min, flash_Container.Conditions_ecu_hw_max);
				if (flag)
				{
					MessageBox.Show(exitReason, ConfigData.WizardSwName);
				}
				return false;
			}
			if (!Evaluate_WizardVersion(ConfigData.WizardSwVersion, flash_Container.Conditions_wizard_min, flash_Container.Conditions_wizard_max))
			{
				exitReason = string.Format("Wizard version not supported, must be version from {0} to {1}\r\nFlash process aborted!" + flash_Container.Conditions_wizard_min, flash_Container.Conditions_wizard_max);
				if (flag)
				{
					MessageBox.Show(exitReason, ConfigData.WizardSwName);
				}
				return false;
			}
			if (!Evaluate_SWVersion(iECU_Data))
			{
				exitReason = string.Format("Current ECU-SW version not supported for update by this flash container, must be in range {0} to {1}\r\nFlash process aborted!", flash_Container.Conditions_ecu_sw_min, flash_Container.Conditions_ecu_sw_max);
				if (flag)
				{
					MessageBox.Show(exitReason, ConfigData.WizardSwName);
				}
				return false;
			}
			if (!Evaluate_DS_Version(iECU_Data))
			{
				exitReason = string.Format("Current ECU data set version not supported for update by this flash container, must be in range {0} to {1}\r\nFlash process aborted!", flash_Container.Conditions_ecu_ds_min, flash_Container.Conditions_ecu_ds_max);
				if (flag)
				{
					MessageBox.Show(exitReason, ConfigData.WizardSwName);
				}
				return false;
			}
			if (!Evaluate_EngineType(iECU_Data))
			{
				exitReason = string.Format("Engine type not supported, must be: {0}\r\nFlash process aborted!", flash_Container.Conditions_engType);
				if (flag)
				{
					MessageBox.Show(exitReason, ConfigData.WizardSwName);
				}
				return false;
			}
			if (Flash_Container.Conditions_country != null && Flash_Container.Conditions_OEM != null && Flash_Container.Conditions_country == iECU_Data.EngineCountry && Flash_Container.Conditions_country == iECU_Data.EngineCountry)
			{
				exitReason = "Engine type not supported #!!!";
				return false;
			}
			if (Flash_Container.Min_serial_motor_nr != null && Flash_Container.Max_serial_motor_nr != null && Flash_Container.Min_serial_motor_nr.Length > 1 && Flash_Container.Max_serial_motor_nr.Length > 1 && !testmotornr.testcMotorSerialNumber(iECU_Data.EngineSerialNumber, Flash_Container.Min_serial_motor_nr, Flash_Container.Max_serial_motor_nr))
			{
				exitReason = "This is a flash container for a specific serial numbers of engines.\r\nThis engine number is out of range.\r\nFlash process aborted!";
				if (flag)
				{
					MessageBox.Show(exitReason, ConfigData.WizardSwName);
				}
				return false;
			}
			if (!Evaluate_BusVoltage(eng))
			{
				exitReason = "Bus voltage too low!\r\nFlash process aborted!";
				if (flag)
				{
					MessageBox.Show(exitReason, ConfigData.WizardSwName);
				}
				return false;
			}
			return true;
		}

		private bool Evaluate_WizardVersion(string wizardVersion, string minVersion, string maxVersion)
		{
			bool result = false;
			try
			{
				string[] array = minVersion.Split('.');
				string[] array2 = maxVersion.Split('.');
				string[] array3 = wizardVersion.Split('.');
				uint num = 0u;
				uint num2 = 0u;
				uint num3 = 0u;
				if (array.Length == 4 || array2.Length == 4 || array3.Length == 4)
				{
					for (int i = 0; i < array3.Length; i++)
					{
						num *= 1000;
						num2 *= 1000;
						num3 *= 1000;
						num += Convert.ToUInt32(array[i]);
						num2 += Convert.ToUInt32(array2[i]);
						num3 += Convert.ToUInt32(array3[i]);
					}
					if (num3 >= num && num3 <= num2)
					{
						result = true;
					}
				}
				return result;
			}
			catch
			{
				return false;
			}
		}

		private bool Evaluate_HWVersion(IECU_Data ecu)
		{
			bool result = false;
			if (ConfigData.hwECUHWTable.ContainsKey(ecu.EcuHwVersionReadable))
			{
				uint num = ConfigData.hwECUHWTable[ecu.EcuHwVersionReadable];
				uint num2 = Convert.ToUInt32(flash_Container.Conditions_ecu_hw_min);
				uint num3 = Convert.ToUInt32(flash_Container.Conditions_ecu_hw_max);
				if (num <= num3 && num >= num2)
				{
					if (fctVer > 0.4f)
					{
						if (flash_Container.FlashBlockAreas[flash_Container.FlashBlockAreas.Count - 1].fdType > FlashBlock.EFD_Types.eFD_DS_2_AE_P)
						{
							if (num > ConfigData.hwECUHWTable["AE"])
							{
								result = true;
							}
						}
						else if (num <= ConfigData.hwECUHWTable["AE"])
						{
							result = true;
						}
					}
					else
					{
						result = true;
					}
				}
			}
			return result;
		}

		private bool Evaluate_FCTVersion(float fctversion)
		{
			bool flag = false;
			fctVer = fctversion;
			if ((double)fctVer >= 0.3 && (double)fctVer < 1.0)
			{
				return true;
			}
			throw new Exception("Evaluate_SWVersion: Wizard not able to read file FCT Ver. " + flash_Container.File_fct_version);
		}

		private bool Evaluate_SWVersion(IECU_Data ecu)
		{
			bool flag = false;
			if (!flash_Container.Conditions_ecu_sw_max.StartsWith("VC"))
			{
				throw new Exception("Evaluate SW version: sw limits of flash container in invalid format!");
			}
			if (!ecu.EcuSwVersion.StartsWith("VC"))
			{
				throw new Exception("Evaluate ECU SW version: has invalid format( VC )!");
			}
			if (ecuSWData.readSwVersion(ecu.EcuSwVersion))
			{
				ushort num = Convert.ToUInt16(flash_Container.Conditions_ecu_sw_max.Substring(2, 2));
				ushort num2 = Convert.ToUInt16(flash_Container.Conditions_ecu_sw_max.Substring(5, 2));
				ushort num3 = Convert.ToUInt16(flash_Container.Conditions_ecu_sw_min.Substring(2, 2));
				ushort num4 = Convert.ToUInt16(flash_Container.Conditions_ecu_sw_min.Substring(5, 2));
				if (ecuSWData.ecu_SW_Version <= num && ecuSWData.ecu_SW_Version >= num3 && ecuSWData.ecu_SW_Revision <= num2 && ecuSWData.ecu_SW_Revision >= num4)
				{
					return true;
				}
				throw new Exception("Evaluate ECU SW version: Is not in SW version/revision area!");
			}
			throw new Exception("Evaluate ECU SW version: has invalid format!");
		}

		public bool Evaluate_SWVersion_Equal(ConfigData.e_ECUID ecuID, FlashContainer fct)
		{
			flash_Container = fct;
			IECU_Data iECU_Data;
			switch (ecuID)
			{
			case ConfigData.e_ECUID.A:
				iECU_Data = ecuA;
				break;
			case ConfigData.e_ECUID.B:
				iECU_Data = ecuB;
				break;
			default:
				MessageBox.Show("Invalid ECU ID selected.\r\nFlash process aborted!", ConfigData.WizardSwName);
				return false;
			}
			if (ConfigFlash.IgnoreFlashCond || fct.Conditions_ignore)
			{
				return true;
			}
			if (flash_Container == null)
			{
				return false;
			}
			string text = iECU_Data.EcuSwVersion.Substring(iECU_Data.EcuSwVersion.IndexOf("VC"), 12);
			if (!flash_Container.Conditions_sw_v.StartsWith("VC") || flash_Container.Conditions_sw_v.Length != 7)
			{
				throw new Exception("Evaluate ECU SW version equal: SW version number of flash container has invalid format!");
			}
			ushort num = Convert.ToUInt16(text.Substring(2, 2));
			ushort num2 = Convert.ToUInt16(text.Substring(7, 2));
			ushort num3 = Convert.ToUInt16(flash_Container.Conditions_sw_v.Substring(2, 2));
			ushort num4 = Convert.ToUInt16(flash_Container.Conditions_sw_v.Substring(5, 2));
			if (num == num3 && num2 == num4)
			{
				return true;
			}
			return false;
		}

		private bool Evaluate_BusVoltage(IEngine12 eng)
		{
			bool result = true;
			if (eng.BatVolt < (double)flash_Container.Conditions_bus_volts_min)
			{
				result = false;
			}
			return result;
		}

		private bool Evaluate_DS_Version(IECU_Data ecu)
		{
			bool result = false;
			try
			{
				ushort num = (ushort)flash_Container.Conditions_ecu_ds_min;
				ushort num2 = (ushort)flash_Container.Conditions_ecu_ds_max;
				if (ecuSWData.ecu_DS_Version >= (double)(int)num && ecuSWData.ecu_DS_Version <= (double)(int)num2)
				{
					result = true;
				}
				return result;
			}
			catch
			{
				return false;
			}
		}

		private bool Evaluate_EngineType(IECU_Data ecu)
		{
			bool result = false;
			if (fctVer > 0.4f)
			{
				if (ecu.EngineType == flash_Container.fdTypeStr)
				{
					result = true;
				}
			}
			else if (ecu.EngineType == flash_Container.Conditions_engType)
			{
				result = true;
			}
			return result;
		}
	}
}
